package Chatnetwork1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CopyOnWriteArrayList;

//创建聊天服务
public class ChatServer {

    private static CopyOnWriteArrayList<Channel> all = new CopyOnWriteArrayList<>();

    public static void main(String[] args) throws IOException {
        System.out.println("------服务端--------");
        //1.创建服务器
        ServerSocket server = new ServerSocket(8889);
        //2.链接accept
        while (true) {
            Socket client = server.accept();
            System.out.println("一个客户建立了一个链接");
            Channel c = new Channel(client);
            all.add(c);
            new Thread(c).start();
        }
    }

    static class Channel implements Runnable {
        private DataInputStream dis;
        private DataOutputStream dos;
        private Socket client;
        private boolean isRunning;
        private String name;

        public Channel(Socket client) {
            isRunning = true;
            this.client = client;
            try {
                dis = new DataInputStream(client.getInputStream());
                dos = new DataOutputStream(client.getOutputStream());
                //获取名称
                this.name = receive();
                this.send("欢迎上线");
                this.sendOthers(this.name + "上线了", true);
            } catch (IOException e) {
                release();
            }
        }

        //接收消息
        public String receive() {
            String msg = "";
            try {
                msg = dis.readUTF();
            } catch (IOException e) {
                release();
            }
            return msg;
        }

        //发送消息
        public void send(String msg) {
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void run() {
            while (isRunning) {
                String msg = receive();
                if (!msg.equals("")) {
                    sendOthers(msg, false);
                }
            }
        }

        //关闭资源
        public void release() {
            this.isRunning = false;
            Utils.close(dis, dos, client);
            //退出
            all.remove(this);
            sendOthers(this.name + "下线了", true);
        }

        public void sendOthers(String msg, boolean isSys) {
            boolean isPrivate = msg.startsWith("@");
            if (isPrivate) {
                int idx = msg.indexOf(":");
                //获取目标和数据
                String targetName = msg.substring(1, idx);
                msg = msg.substring(idx + 1);
                for (Channel other : all) {
                    if (other.name.equals(targetName)) {
                        //查找要私聊的信息名
                        other.send(this.name + "对你说" + msg);
                        break;
                    }
                }
            } else {
                for (Channel other : all) {
                    if (this == other) {
                        continue;
                    }
                    if (!isSys) {
                        other.send(this.name + "对大家说" + msg);
                    } else {
                        other.send(msg);
                    }
                }
            }
        }
    }
}
